package com.thinkingstar.iads.datacenter.entity.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.security.auth.callback.Callback;

import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.thinkingstar.iads.common.jsonUtils.AjaxResult;
import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.common.utils.SwapUtil;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignConstantVersion;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignFormulaData;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignFormulaVersion;

/**
 * @ClassName: DaoDcProjectDesignConstantVersion 
 * @Description:数据中心_公式_公式版本库维护
 * @author 1157372219@qq.com 
 * @date 2018年3月19日   
 */
@IocBean(args = {"refer:dao"})
public class DaoDcProjectDesignFormulaVersion extends EntityRepositoryServiceImpl<DcProjectDesignFormulaVersion>{

	public DaoDcProjectDesignFormulaVersion(Dao dao) {
		super(dao);
	}
	
	//查询公式版本库
	public QueryResult findDesignFormulaVersion(NutMap nutMap){
		List <Map> page_list = null;
		Pager pager = null;
		String sqlStr = "select dfv.* , dpdp.project_design_phase_name , dpdc.project_design_content_name , dpdm.project_design_major_name  "
				+ " from dc_formula_version dfv "
				+ " left join dc_project_design_phase dpdp on dfv.project_design_phase_id = dpdp.id"
				+ " left join dc_project_design_content dpdc on dfv.project_design_content_id = dpdc.id "
				+ " left join dc_project_design_major dpdm on dfv.project_design_major_id = dpdm.id "
				+ " where dfv.del_flag = '0' and dpdp.del_flag = '0' and dpdc.del_flag = '0' and dpdm.del_flag = '0' ";
		if(nutMap.get("formula_table_name") != null){
			sqlStr +=" and dfv.formula_table_name like @formula_table_name";
		}
		StringBuffer find_sql = new StringBuffer(sqlStr
				+ " order by dfv.create_time desc ");
		Sql sqls = Sqls.create(find_sql.toString());
		if(nutMap.get("formula_table_name") != null){
			sqls.params().set("formula_table_name","%"+nutMap.get("formula_table_name")+"%");
		}
		if(nutMap.get("page_num") != null && nutMap.get("page_size") != null){
			pager = new Pager(nutMap.getInt("page_num"), nutMap.getInt("page_size"));
			pager.setRecordCount(findDesignFormulaVersionNum());
			sqls.setPager(pager);
		}
		sqls.setCallback(new SqlCallback() {
			List<Map> list = new LinkedList<>();
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				while (rs.next()){
					Map <String , Object> map = new HashMap<>();
					map.put("id", rs.getString("id"));
					map.put("formula_table_code", rs.getString("formula_table_code"));
					map.put("formula_table_name", rs.getString("formula_table_name"));
					map.put("formula_table_version_number", rs.getString("formula_table_version_number"));
					map.put("project_design_phase_name", rs.getString("project_design_phase_name"));
					map.put("project_design_content_name", rs.getString("project_design_content_name"));
					map.put("project_design_major_name", rs.getString("project_design_major_name"));
					map.put("create_time", rs.getString("create_time"));
					map.put("update_time", rs.getString("update_time"));
					map.put("del_flag", rs.getBoolean("del_flag"));
					list.add(map);
				}
				SwapUtil.clearNull(list);
				return list;
			}
		});
		dao().execute(sqls);
		page_list = sqls.getList(Map.class);
		return new QueryResult(page_list, pager);
	}
	
	public Object findDesignFormulaByContentIdAndPhaseId(NutMap param){
		Criteria criteria = Cnd.NEW();
		criteria.where().and("del_flag", "=", false);
		if(param.get("phase_id") != null){
			criteria.where().and("project_design_phase_id", "=", param.get("phase_id"));
		}
		if(param.get("major_id") != null){
			criteria.where().and("project_design_major_id", "=", param.get("major_id"));
		}
		if(param.get("content_id") != null){
			criteria.where().and("project_design_content_id", "=", param.get("content_id"));
		}
		List <DcProjectDesignFormulaVersion> data_list = query(criteria);
		return data_list;
	}
	
	
	//查询公式版本库数量
	public int findDesignFormulaVersionNum(){
		StringBuffer find_sql = new StringBuffer("select count(dfv.id) num"
				+ " from dc_formula_version dfv "
				+ " left join dc_project_design_phase dpdp on dfv.project_design_phase_id = dpdp.id"
				+ " left join dc_project_design_content dpdc on dfv.project_design_content_id = dpdc.id "
				+ " left join dc_project_design_major dpdm on dfv.project_design_major_id = dpdm.id "
				+ " where dfv.del_flag = '0' and dpdp.del_flag = '0' and dpdc.del_flag = '0' and dpdm.del_flag = '0' ");
		Sql sql = Sqls.create(find_sql.toString());
		sql.setCallback(new SqlCallback() {
			int count = 0;
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				while (rs.next())
				count = rs.getInt("num");
				return count;
			}
		});
		dao().execute(sql);
		return sql.getInt();
	}
	
	//查询公式库版本编号
	public String findVersionNumberByCondtion(DcProjectDesignFormulaVersion dcProjectDesignFormulaVersion){
		Condition cnd = Cnd.where("del_flag", "=", "0").and("formula_table_code", "=", dcProjectDesignFormulaVersion.getFormula_table_code())
		.and("formula_table_name", "=", dcProjectDesignFormulaVersion.getFormula_table_name())
		.and("project_design_phase_id", "=", dcProjectDesignFormulaVersion.getProject_design_phase_id())
		.and("project_design_content_id", "=", dcProjectDesignFormulaVersion.getProject_design_content_id())
		.and("project_design_major_id", "=", dcProjectDesignFormulaVersion.getProject_design_major_id()).limit(1, 1).desc("create_time");
		DcProjectDesignFormulaVersion result = fetch(cnd);
		if(result != null){
			return result.getFormula_table_version_number();
		}
		return null;
	}

	/**
	 * 根据条件(阶段id,专业id,设计内容id)查询符合条件的 公式集列表
	 * @param nutMap
	 * @return
	 */
	public List<DcProjectDesignFormulaVersion> findFormulaVersionList(NutMap nutMap){
		List <Map> list = null;
		String find_sql = " select * from dc_formula_version where project_design_content_id=@content_id and project_design_major_id=@major_id and project_design_phase_id=@phase_id " +
				"and del_flag='0' order by formula_table_version_number desc ";
		Sql sql = Sqls.create(find_sql);
		sql.params().set("content_id", nutMap.get("content_id"));
		sql.params().set("major_id", nutMap.get("major_id"));
		sql.params().set("phase_id", nutMap.get("phase_id"));
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(DcProjectDesignFormulaVersion.class));
		dao().execute(sql);
		return sql.getList(DcProjectDesignFormulaVersion.class);
	}
	
}
